<template>
	<view class="container" style="margin: 30rpx;">
		<uni-forms ref="form" :rules="{}" :model="user">
			<uni-forms-item label="姓名" name="">
				<uni-easyinput v-model="user.name" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="">
				<uni-easyinput v-model="user.password" type="password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="再次确认密码" name="">
				<uni-easyinput type="password" v-model="verify.password" placeholder="再次输入密码" />
			</uni-forms-item>
			<uni-forms-item label="学院" name="">
				<uni-combox :candidates="verify.department" placeholder="请选择学院" v-model="user.department"></uni-combox>
			</uni-forms-item>
			<uni-forms-item label="专业" name="">
				<uni-combox :candidates="verify.major" placeholder="请选择专业" v-model="user.major"></uni-combox>
			</uni-forms-item>
			<uni-forms-item label="电话" name="">
				<uni-easyinput v-model="user.telephone" placeholder="请输入电话" />
			</uni-forms-item>
			<uni-forms-item label="邮箱" name="">
				<uni-easyinput type="text" v-model="user.email" placeholder="请输入邮箱" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="register">注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					password: '',
					telephone: '',
					department: '',
					email: '',
					major: '',
				},
				verify: {
					password: '', // 再次输入密码
					department: ['信息工程学院', '经济管理学院', '智能制造学院'],
					major: ['软件技术', '移动应用开发', '大数据'],
				}
			}
		},
		methods: {
			register(e) {
				if (this.user.telephone && this.user.password !== '' &&
									this.user.password === this.verify.password
								) {
									uni.request({
										url: 'http://localhost:8090/freshmenapp/user/register',
										method: 'post',
										data: this.user,
										success: res => {
											console.log(res);
											if (res.data.success === true) {
												uni.showToast({
													title: this.user.name + '注册成功',
													icon: 'success'
												});
											} else {
												uni.showToast({
													title: '注册失败',
													icon: 'error'
												});
											}
										},
										fail: (err) => {
											uni.showToast({
												title: '网络错误',
												icon: 'error'
											});
										},
										complete: () => {}
									});
								} else {
									uni.showToast({
										title: '用户名或密码错误',
										icon: 'error'
									});
				}
			}
		}
	}
</script>

<style>

</style>